# -*- encoding:utf-8 -*-
# @time:2023-05-09 21:01
# @Author : serein
# @Email: serein@qq.com
# @File:serializers.py
# @Software: PyCharm

from rest_framework import serializers

from shopping.models import GoodsType, CommodityImg, Commodity


class GoodsTypeSerializers(serializers.ModelSerializer):
    parent = serializers.CharField(source='parent.name',read_only=True)
    class Meta:
        model = GoodsType
        fields = ['id', 'name','parent']

class ParentSerializers(serializers.ModelSerializer):
    goodstype_set = GoodsTypeSerializers(many=True,read_only=True)
    class Meta:
        model = GoodsType
        fields = ['id', 'name','parent','goodstype_set']
    extra_kwargs = {
        'parent': {'write_only': True}
    }

#创建商品图片序列化器，只展示图片路径
class CommodityImgSerializer(serializers.ModelSerializer):
    class Meta:
        model = CommodityImg
        fields = ['img_url']

#创建商品数据序列化器，重写关联的商品类型的数据，展示商品类型的名称，而不是id，以及调用商品图片的序列化器，
class CommoditySerializer(serializers.ModelSerializer):
    classification1_name = serializers.CharField(source='classification1.name', read_only=True)
    classification2_name = serializers.CharField(source='classification2.name', read_only=True)
    commodityimg_set = CommodityImgSerializer(many=True, read_only=True)

    class Meta:
        model = Commodity
        exclude = ['is_delete']


